package com.lantosec.hmrp.modules.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.system.entity.SysPermission;
import com.lantosec.hmrp.modules.system.service.ISysPermissionService;
import com.lantosec.hmrp.modules.system.vo.SysPermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 系统权限表 控制器
 *
 * @author AutoCode
 * @since 2020-02-15
 */
@Api(value = "系统权限表控制器", tags = "系统权限表控制器")
@RestController
@RequestMapping("/permission")
@Log4j2
public class SysPermissionController extends BaseController {
    @Autowired
    private ISysPermissionService service;

    @GetMapping("queryPermissionById")
    @ApiOperation("根据权限ID查询下一级权限")
    public Result<Page<SysPermissionVO>> queryPermissionByName(PageVO<SysPermission> page,
             @ApiParam(value = "权限名称", required = true) @RequestParam(name = "permissionId") String permissionId
    ){
        Page<SysPermission> permissionPage = service.queryPermissionById(permissionId, page);
        return Result.ok(entity2VO(permissionPage));
    }

    @GetMapping("queryOnlyPermissionName")
    @ApiOperation("权限名称唯一性校验")
    public Result<Integer> queryOnlyPermissionName(
            @ApiParam(value = "权限名称", required = true) @RequestParam(name = "permissionName") String permissionName
    ){
        Integer param = service.queryOnlyPermissionName(permissionName);
        if(param != 0){
            return Result.fail("权限名称已存在!");
        }else {
            return Result.ok(param);
        }
    }

    @PutMapping("changeState")
    @ApiOperation("禁用/启用")
    public Result changeState(
            @ApiParam(value = "权限ID", required = true) @RequestParam(name = "permissionId") String permissionId,
            @ApiParam(value = "状态", required = true) @RequestParam(name = "state") String state
    ){
        service.changeState(permissionId,state);
        return Result.ok();
    }

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysPermissionVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysPermissionVO> get(
            @ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("createPermissionInfo")
    @ApiOperation(value = "新增数据")
    public Result<String> create(
            @ApiParam(value = "系统权限表VO对象", required = true) @RequestBody @Validated SysPermissionVO vo, BindingResult bindingResult){
        validationHandler(bindingResult);
        SysPermission entity = vo.convertToEntity();
        String permissionId = service.queryMaxId(entity.getParentId());
        entity.setPermissionId(permissionId);
        service.save(entity);
        return Result.ok(entity.getPermissionId());
    }

    @PutMapping("updatePermissionInfo")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "系统权限表VO对象", required = true) @RequestBody SysPermissionVO vo){
        SysPermission entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @GetMapping("queryTopPermissionList")
    @ApiOperation("查询左侧父级权限列表")
    public Result<List<SysPermissionVO>> queryTopPermissionList(){
        List<SysPermission> param = service.queryTopPermissionList();
        return Result.ok(entity2VO(param));
    }

    @GetMapping("getPermissionTree")
    @ApiOperation("查询权限树")
    public Result<List<TreeNodeVo>> getAreaTree() {
        return Result.ok(service.getPermissionTree());
    }

}
